<?php
/**
 *  This class has useful actions for a developer 
 *	@author Alecsandru Gabriel DINU
 *  @since Jun 18, 2008
 *  @Bean(id = "developmentAction")  
 */
class DevelopmentAction extends BaseAction {
	private $command;
	private $model;
	private $schema;

	public function setCommand($cmd) {$this->command = $cmd;}
	public function getCommand() {return $this->command;}
	
	public function setModel($model) {$this->model = $model;}
	public function getModel() {return $this->model;}

	public function setSchema($schema) {$this->schema = $schema;}
	public function getSchema() {return $this->schema;}
	
	public function index() {
		$this->command = "none";
		return SUCCESS;
	}

	public function reload() {
		try {
			AlfaController::getInstance(true)->reload();
			AlfaCacheManager::getInstance()->reload();
			$this->addSuccessMessage("Configuration successfully reloaded!");
		} catch (Exception $e) {
			$this->addErrorMessage("Configuration could not be reloaded!");	
		}

		return SUCCESS;
	}

	public function execDb() {
		$databaseManager = new DatabaseManager();
		$databaseManager->execute($this->command);
		return SUCCESS;
	}
	
	public function tbs() {
		return SUCCESS;
	}
	
	public function importDbDesigner() {
		$schema = $this->schema;
		$model = $this->model;
		
		if (!eregi("(.*){1}\.(.*){1}", $schema)) {
			$schema .= ".xml"; 
		}

		if (!eregi("(.*){1}\.(.*){1}", $model)) {
			$model .= ".xml"; 
		}
		
		$schema = $this->config->getString('build/path/schema') . "/$schema";
		$model = $this->config->getString('build/path/schema') . "/$model";
		
		if (!file_exists($model)) {
			$this->addErrorMessage("DB Designer model file could not be located");
			return ERROR;
		}
		
		
		$databaseManager = new DatabaseManager();
		try {
			$databaseManager->importDbDesignerModel($model, $schema);
			$this->addSuccessMessage("Import performed successful.");
		} catch (FileNotFoundException  $e) {
			$this->addErrorMessage("Import could not be performed");
			return SUCCESS;	
		}
		
		return SUCCESS;
	}
}
?>